package com.ruoyi.sl.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.domain.SlWorkshop;
import com.ruoyi.sl.service.ISlModuleService;
import com.ruoyi.sl.service.ISlWorkshopService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlGprocess;
import com.ruoyi.sl.service.ISlGprocessService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 大工序Controller
 * 
 * @author admin
 * @date 2024-06-05
 */
@RestController
@RequestMapping("/sl/gprocess")
public class SlGprocessController extends BaseController
{
    @Autowired
    private ISlGprocessService slGprocessService;

    @Autowired
    private ISlModuleService slModuleService;

    @Autowired
    private ISlWorkshopService slWorkshopService;

    /**
     * 查询大工序列表
     */
    @PreAuthorize("@ss.hasPermi('sl:gprocess:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlGprocess slGprocess)
    {
        startPage();
        List<SlGprocess> list = slGprocessService.selectSlGprocessList(slGprocess);
        return getDataTable(list);
    }

    /**
     * 根据车间ID查询所有大工序
     */
    @GetMapping(value = "/glist/{workshopId}")
    public AjaxResult getGprocesslistByWorkshopId(@PathVariable Long workshopId)
    {
        return success(slGprocessService.selectSlGprocessListByWorkshopId(workshopId));
    }

    /**
     * 导出大工序列表
     */
    @PreAuthorize("@ss.hasPermi('sl:gprocess:export')")
    @Log(title = "大工序", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlGprocess slGprocess)
    {
        List<SlGprocess> list = slGprocessService.selectSlGprocessList(slGprocess);
        ExcelUtil<SlGprocess> util = new ExcelUtil<SlGprocess>(SlGprocess.class);
        util.exportExcel(response, list, "大工序数据");
    }

    /**
     * 获取大工序详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:gprocess:query')")
    @GetMapping(value = {"/","/{gprocessId}"})
    public AjaxResult getInfo(@PathVariable(value = "gprocessId", required = false) Long gprocessId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("modules", slModuleService.selectModuleAll());
        if (StringUtils.isNotNull(gprocessId))
        {
            SlGprocess slGprocess = slGprocessService.selectSlGprocessByGprocessId(gprocessId);
            ajax.put(AjaxResult.DATA_TAG, slGprocess);
            //获取模块对应的车间
            if(StringUtils.isNotNull(slGprocess.getModuleId())){
                ajax.put("workshops", slWorkshopService.selectSlWorkshopListByModuleId(slGprocess.getModuleId()));
            }
            //ajax.put("workshop",slWorkshopService.selectSlWorkshopList());
        }
        return ajax;
        //return success(slGprocessService.selectSlGprocessByGprocessId(gprocessId));
    }

    /**
     * 新增大工序
     */
    @PreAuthorize("@ss.hasPermi('sl:gprocess:add')")
    @Log(title = "大工序", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlGprocess slGprocess)
    {
        if (!slGprocessService.checkGprocessName(slGprocess))
        {
            return error("添加失败,大工序名'" + slGprocess.getGprocessName() + "'已存在");
        }
        slGprocess.setCreateBy(getUsername());
        return toAjax(slGprocessService.insertSlGprocess(slGprocess));
    }

    /**
     * 修改大工序
     */
    @PreAuthorize("@ss.hasPermi('sl:gprocess:edit')")
    @Log(title = "大工序", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlGprocess slGprocess)
    {
        if (!slGprocessService.checkGprocessName(slGprocess))
        {
            return error("修改失败,大工序名'" + slGprocess.getGprocessName() + "'已存在");
        }
        slGprocess.setUpdateBy(getUsername());
        return toAjax(slGprocessService.updateSlGprocess(slGprocess));
    }

    /**
     * 删除大工序
     */
    @PreAuthorize("@ss.hasPermi('sl:gprocess:remove')")
    @Log(title = "大工序", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gprocessIds}")
    public AjaxResult remove(@PathVariable Long[] gprocessIds)
    {
        for (int i = 0; i < gprocessIds.length; i++) {
            if(!slGprocessService.checkGprocessMemberNum(gprocessIds[0])){
                return error("该大工序下有小工序存在，删除失败。");
            }
        }

        return toAjax(slGprocessService.deleteSlGprocessByGprocessIds(gprocessIds));
    }
}
